package com.leo.base_project.Utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import com.leo.base_project.config.App;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * @program: BaseProject
 * @description: 系统界面相关工具类
 * @author: Leo
 * @create: 2021-03-28 11:08
 **/
public class UIUtils {

    /* @Description: 短的弹窗
     * @Param: text 文字
     * @return: void
     * @Author: Leo
     * @Date: 2021/3/29
     **/
    public static void toast(String text) {
        Toast
                .makeText(App.getAppContext(), text, Toast.LENGTH_SHORT)
                .show();
    }

    /* @Description: 1s的弹窗
     * @Param: text 文字
     * @return: void
     * @Author: Leo
     * @Date: 2021/3/29
     **/
    public static void toast_long(String text) {
        Toast
                .makeText(App.getAppContext(), text, Toast.LENGTH_LONG)
                .show();
    }

    public static String formatDate(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(date);
    }

    /* @Description: 获取当前app version code
     * @Param: [context] activity的this即可
     * @return long
     * @Author: Leo
     * @Date: 2021/3/29
     **/
    public static long getAppVersionCode(Context context) {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("", Objects.requireNonNull(e.getMessage()));
        }
        return appVersionCode;
    }

    /* @Description: 获取当前app version name
     * @Param: [context] activity的this即可
     * @return java.lang.String
     * @Author: Leo
     * @Date: 2021/3/29
     **/
    public static String getAppVersionName(Context context) {
        String appVersionName = "";
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            appVersionName = packageInfo.versionName;
            appVersionName =  new String(appVersionName.getBytes("gb2312"), StandardCharsets.UTF_8);
        } catch (PackageManager.NameNotFoundException | UnsupportedEncodingException e) {
            Log.e("", Objects.requireNonNull(e.getMessage()));
        }
        return appVersionName;
    }

}
